package com.hzdesk.media.koreabatchmatvector.logic.persenter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.hzdesk.media.koreabatchmatvector.adapter.MouseStrokesAdapter;
import com.hzdesk.media.koreabatchmatvector.adapter.OptionAdapter;
import com.hzdesk.media.koreabatchmatvector.config.AppConfig;
import com.hzdesk.media.koreabatchmatvector.inter.OnOptionItemClickListener;
import com.hzdesk.media.koreabatchmatvector.logic.contract.OptionContract;
import com.hzdesk.media.koreabatchmatvector.utils.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Administrator on 2018/7/7.
 */

public class OptionPersenter implements OptionContract.IOptionPersenter {
    private OptionContract.IOptionView mIOptionView;

    private ArrayList<String> subsetionTimeList=new ArrayList<>();
    private ArrayList<String> recordModeList=new ArrayList<>();
    private ArrayList<String> userModeList=new ArrayList<>();
    private ArrayList<String> protocolModeList=new ArrayList<>();
    private ArrayList<String> titleList=new ArrayList<>();
    private List<String> datalist_1 = new LinkedList<>(Arrays.asList("红", "橙", "黄", "绿", "天蓝", "蓝", "紫", "黑"));
    private List<Integer> datalist_2 = new LinkedList<>(Arrays.asList( 3, 5, 7, 9));                                                         //笔触下拉框数据
    private RecyclerView mRecyclerView;
    private OptionAdapter optionAdapter;
    private MouseStrokesAdapter mouseStrokesAdapter;
    private Context mContext;
    private String mode;


    public OptionPersenter(OptionContract.IOptionView iOptionView) {
        mIOptionView=iOptionView;


    }

    @Override
    public void initView(Context context,Intent intent, RecyclerView recyclerView) {
        initOptionList();
        mContext=context;
        Intent intent1=intent;
        mRecyclerView=recyclerView;
        LinearLayoutManager layoutmanager = new LinearLayoutManager(mContext);
        layoutmanager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutmanager);
        optionAdapter=new OptionAdapter(mContext);
        mouseStrokesAdapter=new MouseStrokesAdapter(mContext);
        mode=intent1.getStringExtra("Option");
        //Log.e("ChangeMouseSetPersenter", "sharedPreferencesUtils  **** showCursorAndPathSize mode: "+mode );
        //分段时长
        if (mode.equals(AppConfig.OPTION_SUBSETION_TIME)){
            int s= (int) ((SharedPreferencesUtils.getSubSectionTime(mContext)/60/1000/5)-1);
            mIOptionView.setTitle(titleList.get(0));
            optionAdapter.addItemDate(subsetionTimeList);
            optionAdapter.setSelectPostion(s);
            mRecyclerView.setAdapter(optionAdapter);
            setOnItemClickListener();

        }
        //录制模式
        if (mode.equals(AppConfig.OPTION_RECORD_MODE)){
            int s=SharedPreferencesUtils.getRecordMode(mContext);
            mIOptionView.setTitle(titleList.get(1));
            optionAdapter.addItemDate(recordModeList);
            optionAdapter.setSelectPostion(s);
            mRecyclerView.setAdapter(optionAdapter);
            setOnItemClickListener();
        }
        //使用模式
        if (mode.equals(AppConfig.OPTION_USER_MODE)){
            int s=SharedPreferencesUtils.getUserMode(mContext);
            mIOptionView.setTitle(titleList.get(2));
            optionAdapter.addItemDate(userModeList);
            optionAdapter.setSelectPostion(s);
            mRecyclerView.setAdapter(optionAdapter);
            setOnItemClickListener();
        }
        //协议
        if (mode.equals(AppConfig.OPTION_PROTOCOL)){
            int s=SharedPreferencesUtils.getProtocal(mContext);
            mIOptionView.setTitle(titleList.get(3));
            optionAdapter.addItemDate(protocolModeList);
            optionAdapter.setSelectPostion(s);
            mRecyclerView.setAdapter(optionAdapter);
            setOnItemClickListener();
        }
        // 光标颜色
        if(mode.equals(AppConfig.OPTION_CURSOR_COLOR)){
            int s=SharedPreferencesUtils.getCursorColor(mContext);
            mIOptionView.setTitle(titleList.get(4));
            optionAdapter.addItemDate(datalist_1);
            optionAdapter.setSelectPostion(s);
            mRecyclerView.setAdapter(optionAdapter);
            setOnItemClickListener();
        }
        // 笔触大小
        if(mode.equals(AppConfig.OPTION_path_size)){
            int s=SharedPreferencesUtils.getPathSize(mContext);
            mIOptionView.setTitle(titleList.get(5));
            mouseStrokesAdapter.addItemDate(datalist_2);
            mouseStrokesAdapter.setSelectPostion(s);
            mRecyclerView.setAdapter(mouseStrokesAdapter);
            mouseStrokesAdapter.setOnOptionItemClickListener(new OnOptionItemClickListener() {
                @Override
                public void OnItemClick(int position) {
                    SharedPreferencesUtils sharedPreferencesUtils=new SharedPreferencesUtils(mContext);
                    sharedPreferencesUtils.putDate(AppConfig.OPTION_path_size,position);
                    //Log.e("ChangeMouseSetPersenter", "sharedPreferencesUtils  **** showCursorAndPathSize PathSizeIndex: "+position );
                    mIOptionView.back();
                }
            });

        }


    }

    private void setOnItemClickListener(){
        optionAdapter.setOnOptionItemClickListener(new OnOptionItemClickListener() {
            @Override
            public void OnItemClick(int position) {
                SharedPreferencesUtils sharedPreferencesUtils=new SharedPreferencesUtils(mContext);
                if (mode.equals(AppConfig.OPTION_SUBSETION_TIME)){
                    long subsetionTime=(position+1)*5*60*1000;
                    sharedPreferencesUtils.putDate(AppConfig.OPTION_SUBSETION_TIME,subsetionTime);
                    mIOptionView.back();
                }
                if (mode.equals(AppConfig.OPTION_RECORD_MODE)){
                    sharedPreferencesUtils.putDate(AppConfig.OPTION_RECORD_MODE,position);
                    mIOptionView.back();

                }
                if (mode.equals(AppConfig.OPTION_USER_MODE)){
                    sharedPreferencesUtils.putDate(AppConfig.OPTION_USER_MODE,position);
                    mIOptionView.back();

                }
                if (mode.equals(AppConfig.OPTION_PROTOCOL)){
                    sharedPreferencesUtils.putDate(AppConfig.OPTION_PROTOCOL,position);
                    mIOptionView.back();

                }
                if(mode.equals(AppConfig.OPTION_CURSOR_COLOR)){
                    sharedPreferencesUtils.putDate(AppConfig.OPTION_CURSOR_COLOR,position);
                    //Log.e("ChangeMouseSetPersenter", "sharedPreferencesUtils  **** showCursorAndPathSize colorIndex: "+position );
                    mIOptionView.back();
                }

            }
        });
    }

    private void initOptionList(){
        //subsetionTimeList.add("1分钟");
        subsetionTimeList.add("5分钟");
        subsetionTimeList.add("10分钟");
        subsetionTimeList.add("15分钟");
        subsetionTimeList.add("20分钟");
        subsetionTimeList.add("25分钟");
        subsetionTimeList.add("30分钟");
        recordModeList.add("信号源");
        recordModeList.add("信号源+麦克风");
        userModeList.add("特权模式");
        userModeList.add("普通模式");
        protocolModeList.add("Miracast");
        protocolModeList.add("HDMI IN");
        titleList.add("分段时长");
        titleList.add("录制模式");
        titleList.add("使用模式");
        titleList.add("投屏协议");
        titleList.add("光标颜色");
        titleList.add("笔触大小");
    }




}
